package erp_services

import (
	"xiehui-service/app/models/erp_models"
	"xiehui-service/global"
)

type materialWeightService struct {
}

var MaterialWeightService = new(materialWeightService)

// 新增
func (materialWeightService *materialWeightService) Create(mw erp_models.MaterialWeight) (uint, error) {
	result := global.App.DB.Create(&mw)
	if result.Error != nil {
		return 0, result.Error
	}
	return mw.ID.ID, nil
}

// 更新
func (materialWeightService *materialWeightService) Update(mw erp_models.MaterialWeight) (err error) {
	if err := global.App.DB.Model(&erp_models.MaterialWeight{}).Where("id = ?", mw.ID.ID).Omit("id", "created_at").Updates(&mw).Error; err != nil {
		return err
	}
	return nil
}

// 根据磅单ID查询磅单信息
func (materialWeightService *materialWeightService) GetMaterialWeightByDH(dh string, data_source int64) (mw *erp_models.MaterialWeight, err error) {
	if err = global.App.DB.Where("weight_no = ?", dh).Where("data_source = ?", data_source).First(&mw).Error; err != nil {
		return nil, err
	}
	return
}
